namespace RushMail.Requests;

/// <summary>
/// 域名相关接口
/// </summary>
public class Domain
{
    private readonly IRushMailClient _client;

    public Domain(IRushMailClient client)
    {
        _client = client;
    }

    /// <summary>
    /// 域名列表
    /// http://edmapi.rushmail.com/api.php?module=domain&action=list
    /// </summary>
    /// <returns></returns>
    public async Task<RushMailResponse<DomainListResponse>> List() =>
        (await _client.PostAsync(new RushMailRequest("domain", "list"))).ToGeneric<DomainListResponse>();
    
    
    /// <summary>
    /// 添加域名
    /// http://edmapi.rushmail.com/api.php?module=domain&action=add
    /// </summary>
    /// <returns></returns>
    public async Task<RushMailResponse<int>> Add(DomainAdd body) =>
        (await _client.PostAsync(new RushMailRequest("domain", "add")
        {
            Body = body.ToDictionary()
        }))
        .ToGeneric<int>();
    
    /// <summary>
    /// 修改域名
    /// http://edmapi.rushmail.com/api.php?module=domain&action=edit
    /// </summary>
    /// <returns></returns>
    public Task<RushMailResponse> Edit(DomainEdit body) =>
        _client.PostAsync(new RushMailRequest("domain", "edit")
        {
            Body = body.ToDictionary()
        });

    /// <summary>
    /// 删除域名
    /// http://edmapi.rushmail.com/api.php?module=domain&action=delete
    /// </summary>
    /// <returns></returns>
    public Task<RushMailResponse> Delete(int domainId) =>
        _client.PostAsync(new RushMailRequest("domain", "delete")
        {
            Body = new Dictionary<string, string>() { {"domain_id", domainId.ToString()} }
        });
    
    /// <summary>
    /// 生成 DKIM 私钥
    /// 注：如果是新添加的域名，必须调用一次（且只需调用一次）本接口
    /// http://edmapi.rushmail.com/api.php?module=domain&action=dkim-generate
    /// </summary>
    /// <returns></returns>
    public Task<RushMailResponse> DkimGenerate(int domainId) =>
        _client.PostAsync(new RushMailRequest("domain", "dkim-generate")
        {
            Body = new Dictionary<string, string>() { {"domain_id", domainId.ToString()} }
        });
    
    /// <summary>
    /// 获取域名解析记录列表
    /// 如果是新添加的域名，必须调用一次（且只需调用一次） 生成 DKIM 私钥 接口， 否则返回的数据中 DKIM 的记录值将为空
    /// http://edmapi.rushmail.com/api.php?module=domain&action=domain-resolution-list
    /// </summary>
    /// <returns></returns>
    public Task<RushMailResponse> ResolutionList(int domainId) =>
        _client.PostAsync(new RushMailRequest("domain", "domain-resolution-list")
        {
            Body = new Dictionary<string, string>() { {"domain_id", domainId.ToString()} }
        });
    
        
    /// <summary>
    /// 验证域名解析
    /// http://edmapi.rushmail.com/api.php?module=domain&action=domain-verify
    /// </summary>
    /// <param name="domainId"></param>
    /// <param name="recordName">A、MX、SPF、DKIM、DMARC</param>
    /// <returns></returns>
    public Task<RushMailResponse> Verify(int domainId, string recordName) =>
        _client.PostAsync(new RushMailRequest("domain", "domain-verify")
        {
            Body = new Dictionary<string, string>() { {"domain_id", domainId.ToString()}, {"recordname", recordName} }
        });
    
}


public class DomainAdd
{
    public string Name { get; set; }
    /// <summary>
    /// -1 禁用 1 正常
    /// </summary>
    public int Status { get; set; }

    public virtual Dictionary<string, string> ToDictionary() => new Dictionary<string, string>()
    {
        { "name", Name },
        { "status", Status.ToString() },
    };
}

public class DomainEdit
{
    public int DomainId { get; set; }
    /// <summary>
    /// -1 禁用 1 正常
    /// </summary>
    public int Status { get; set; }

    public virtual Dictionary<string, string> ToDictionary() => new Dictionary<string, string>()
    {
        { "domain_id", DomainId.ToString() },
        { "status", Status.ToString() },
    };
}

public class DomainListResponse
{
    [JsonProperty("domain_list")]public List<DomainItem> DomainList { get; set; }
}

public class DomainItem
{
    [JsonProperty("domain_id")]
    public int DomainId { get; set; }
    [JsonProperty("domain_name")]public string DomainName { get; set; }
    /// <summary>
    /// 任务状态 -1 禁用 1 正常
    /// </summary>
    public int Status { get; set; }
}
